<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 文章管理 iframe 框</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../layuiadmin/layui/css/layui.css" th:href="@{/layuiadmin/layui/css/layui.css}"
          media="all">
    <link rel="stylesheet" href="../layuiadmin/style/admin.css" th:href="@{/layuiadmin/style/admin.css}" media="all">
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>文章添加</legend>
</fieldset>

<form class="layui-form" method="post">
    <div style="padding: 20px; background-color: #F2F2F2;">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md6">
                <div class="layui-card">
                    <div class="layui-card-header">请填写文章信息</div>
                    <div class="layui-card-body">
                        <div class="layui-form-item">
                            <label class="layui-form-label">文章标题</label>
                            <div class="layui-input-inline">
                                <input type="text" id="title" name="title" lay-verify="required" placeholder="请输入用户名"
                                       autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">发布人</label>
                            <div class="layui-input-inline">
                                <input type="text" id="author" name="author" lay-verify="required" placeholder="请输入发布人"
                                       autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">文章内容</label>
                            <div class="layui-input-inline">
            <textarea id="content" name="content" lay-verify="required" style="width: 400px; height: 150px;"
                      autocomplete="off"
                      class="layui-textarea"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">标签</label>
                            <div class="layui-input-inline">
                                <select id="label" name="label" lay-verify="required">
                                    <option value="">请选择标签</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">发布状态</label>
                            <div class="layui-input-inline">
                                <input type="checkbox" lay-verify="required" lay-filter="status" id="status"
                                       name="status"
                                       lay-skin="switch"
                                       lay-text="已发布|待修改">
                            </div>
                        </div>
                        <button class="layui-btn" lay-submit lay-filter="formBtn"
                                style="margin-left: 120px">立即添加
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

<script src="../layuiadmin/layui/layui.js" th:src="@{/layuiadmin/layui/layui.js}"></script>
<script>
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use('index', function () {
    });
</script>
<script>
    layui.use(['table', 'form'], function (exports) {
        var $ = layui.$, table = layui.table, form = layui.form;
        //动态加载文章类型
        $.ajax({
            url: '/admin/label/list',
            type: 'POST',
            dataType: 'json',
            success: function (result) {
                var list = result;
                for (var i = 0; i < list.length; i++) {
                    //追加option
                    $("#label").append("<option value=" + list[i].id + ">" + list[i].labelName + "</option>");
                    //渲染刷新
                    form.render('select');
                }
            },

        });

        //添加操作
        form.on('submit(formBtn)', function (data) {
            console.log(data);
            //发送ajax请求
            $.ajax({
                url: '/admin/article/add',
                data: JSON.stringify(data.field),
                type: 'POST',
                contentType: 'application/json',  //数据类型格式
                success: function (result) {
                    if (result.success == true) {
                        layer.closeAll();
                        layer.msg('添加成功', {time: 1 * 1000}, function () {
                            location.reload();
                        });
                    } else {
                        layer.closeAll();
                        layer.msg('添加失败', {time: 1 * 1000}, function () {
                            location.reload();
                        });
                    }
                },
                error: function (errorMsg) {
                    alert("数据异常！" + errorMsg);
                    location.reload();
                },
            });
            return false;
        });

    });

</script>
</body>
</html>